﻿using System;
using System.Collections.Generic;
using System.Text;
using Com.Toook.AutoDownloadHtml.Entity;
using NSoup.Select;
using NSoup.Nodes;

namespace Com.Toook.AutoDownloadHtml.DownloadHtml
{
    public class ParseHtmlStep
    {
        public Elements GetElements(Elements elements, Step step)
        {
            Elements resultElements = new Elements();

            for(int i = 0; step.Next() && i < elements.Count; i++ )
            {
                Elements eles = GetBy(elements[i], step);
                if(eles.Count < 1)
                {
                    break;
                }
                foreach(Element ele in eles)
                {
                    resultElements.Add(ele);
                }
            }
            return resultElements;
        }
        /// <summary>
        /// 根据每一步提示获取元素
        /// </summary>
        /// <param name="element"></param>
        /// <param name="step"></param>
        /// <returns></returns>
        private Elements GetBy(Element element, Step step)
        {
            Elements eles = new Elements();
            switch(step.By)
            {
                case ByType.Class:
                    eles = element.GetElementsByClass(step.Name);
                    break;
                case ByType.Tag:
                    eles = element.GetElementsByTag(step.Name);
                    break;
                case ByType.Attr:
                    eles = element.GetElementsByAttribute(step.Name);
                    break;
                case ByType.Id:
                    Element e = element.GetElementById(step.Name);
                    eles.Add(e);
                    break;
                case ByType.AttrStart:
                    eles = element.GetElementsByAttributeStarting(step.Name);
                    break;
                //case ByType.Contain:
                //    eles = element.GetElementsByAttributeValueContaining(step.Name);
                //    break;
                default:
                    break;
            }

            return eles;
        }

        private Step step = new Step();

    }   
}
